package socket

import (
	"fmt"
	"gitee.com/DengAnbang/Wefore/src/api"
	"gitee.com/DengAnbang/Wefore/src/push"

	"gitee.com/DengAnbang/Wefore/src/bean"
	"gitee.com/DengAnbang/Wefore/src/code"
	"gitee.com/DengAnbang/Wefore/src/fun"
)

func Dispense(message *bean.RequestData, conn push.ResponseAble) {
	defer func() {
		i := recover()
		if err, ok := i.(error); ok {
			conn.Response(err, 0)
		}
	}()

	var parameters = message.Data

	//err := json.Unmarshal(message.Data, &parameters)
	//panic(err)
	switch message.Type {
	//用户相关
	case 100:
		userId := fun.GetString(parameters, "userId")
		lastCon := push.Register(userId, conn)
		if lastCon != nil {
			succeedMessage := bean.NewSucceedMessage("账号在其他地方登录了!")
			succeedMessage.Type = code.TypeOtherLogin
			_ = lastCon.SendMessageToConn(succeedMessage)
		}
	case 101:
		conn.Response(api.UserRegisterSocket(parameters), message.Type)
	case 102:
		userId, err := api.UserLoginSocket(parameters)
		lastCon := push.Register(userId, conn)
		if lastCon != nil {
			succeedMessage := bean.NewSucceedMessage("账号在其他地方登录了!")
			succeedMessage.Type = code.TypeOtherLogin
			_ = lastCon.SendMessageToConn(succeedMessage)
		}
		conn.Response(err, message.Type)
	case 103:
		conn.Response(api.UserUpdateSocket(parameters), message.Type)
	case 104:
		conn.Response(api.UserToStructureTreeAddSocket(parameters), message.Type)
	case 105:
		conn.Response(api.UserToStructureTreeSelectAllSocket(parameters), message.Type)
	case 106:
		conn.Response(api.UserSelectAllSocket(parameters), message.Type)
	case 107:
		conn.Response(api.UserSelectByUserIdSocket(parameters), message.Type)
	case 108:
		conn.Response(api.UserDeleteSocket(parameters), message.Type)

	case 201:
		conn.Response(api.PermissionAddSocket(parameters), message.Type)
	case 202:
		conn.Response(api.PermissionUpdateSocket(parameters), message.Type)
	case 203:
		conn.Response(api.PermissionDeleteSocket(parameters), message.Type)
	case 204:
		conn.Response(api.PermissionSelectSocket(parameters), message.Type)
	case 205:
		conn.Response(api.PermissionSelectListSocket(parameters), message.Type)
	case 206:
		conn.Response(api.PermissionToStructureTreeAddSocket(parameters), message.Type)
	case 207:
		conn.Response(api.PermissionToStructureTreeSelectSocket(parameters), message.Type)
	case 208:
		conn.Response(api.CharacterAddSocket(parameters), message.Type)
	case 209:
		conn.Response(api.CharacterSelectAllSocket(parameters), message.Type)
	case 210:
		conn.Response(api.CharacterDeleteSocket(parameters), message.Type)
	case 301:
		conn.Response(api.StructureTreeNodeCreateSocket(parameters), message.Type)
	case 302:
		conn.Response(api.StructureTreeNodeDeleteSocket(parameters), message.Type)
	case 303:
		conn.Response(api.StructureTreeNodeUpdateSocket(parameters), message.Type)
	case 304:
		conn.Response(api.StructureTreeChildNodeSelectSocket(parameters), message.Type)
	case 305:
		conn.Response(api.StructureTreeChildNodeSelectAllSocket(parameters), message.Type)

	case 501:
		conn.Response(api.ProjectTreeNodeCreateSocket(parameters), message.Type)
	case 502:
		conn.Response(api.ProjectTreeNodeDeleteSocket(parameters), message.Type)
	case 503:
		conn.Response(api.ProjectTreeNodeUpdateSocket(parameters), message.Type)
	case 504:
		conn.Response(api.ProjectTreeChildNodeSelectSocket(parameters), message.Type)
	case 505:
		conn.Response(api.ProjectTreeChildNodeSelectAllSocket(parameters), message.Type)
	case 506:
		conn.Response(api.ProjectTreeUserDeleteSocket(parameters), message.Type)
	case 507:
		conn.Response(api.ProjectTreeUserAddSocket(parameters), message.Type)
	case 508:
		conn.Response(api.ProjectTreeUserSelectSocket(parameters), message.Type)
	case 509:
		conn.Response(api.ProjectTreeUserSelectAllSocket(parameters), message.Type)
	case 510:
		conn.Response(api.ProjectTreeNodeAddStructureSocket(parameters), message.Type)
	case 511:
		conn.Response(api.ProjectTreeUserSelectProjectSocket(parameters), message.Type)
	case 512:
		conn.Response(api.ProjectTreeNodeApplyForCreateSocket(parameters), message.Type)

	case 401:
		conn.Response(api.FileTreeNodeCreateSocket(parameters), message.Type)
	case 402:
		conn.Response(api.FileTreeNodeDeleteSocket(parameters), message.Type)
	case 403:
		conn.Response(api.FileTreeNodeUpdateSocket(parameters), message.Type)
	case 404:
		conn.Response(api.FileTreeChildNodeSelectSocket(parameters), message.Type)
	case 405:
		conn.Response(api.FileTreeChildNodeSelectAllSocket(parameters), message.Type)
	case 406:
		conn.Response(api.FileTreeFileAddSocket(parameters), message.Type)
	case 407:
		conn.Response(api.FileTreeFileSelectSocket(parameters), message.Type)
	case 408:
		conn.Response(api.FileTreeFileCommissionedSocket(parameters), message.Type)
	case 409:
		conn.Response(api.FileTreeFileCommissionedSelectSocket(parameters), message.Type)
		//考勤相关
	case 601:
		conn.Response(api.AttendancePunchInSocket(parameters), message.Type)
	case 602:
		conn.Response(api.AttendancePunchOutSocket(parameters), message.Type)
	case 603:
		conn.Response(api.AttendanceSelectSocket(parameters), message.Type)
	case 604:
		conn.Response(api.AttendanceAskForLeaveCreateSocket(parameters), message.Type)
	case 605:
		conn.Response(api.AttendanceAskForLeaveSelectByIdSocket(parameters), message.Type)
	case 606:
		conn.Response(api.AttendanceAskForLeaveSelectByUserIdSocket(parameters), message.Type)
	case 607:
		conn.Response(api.AttendanceAskForLeaveApprovalSocket(parameters), message.Type)
	case 608:
		conn.Response(api.AttendanceAskForLeaveApprovalSelectSocket(parameters), message.Type)
		//消息相关
	case 701:
		conn.Response(api.MessageSelectSocket(parameters), message.Type)
	case 702:
		conn.Response(api.MessageReadSocket(parameters), message.Type)
	case 703:
		conn.Response(api.MessageDeleteSocket(parameters), message.Type)
		//聊天消息相关
	case 801:
		conn.Response(api.ChatMessageSendSocket(parameters), message.Type)
	case 802:
		conn.Response(api.ChatMessageDeleteSocket(parameters), message.Type)
	case 803:
		conn.Response(api.ChatMessageSelectToMeSocket(parameters), message.Type)
		//合同相关
	case 901:
		conn.Response(api.ContractCreateSocket(parameters), message.Type)
	case 902:
		conn.Response(api.ContractDeleteSocket(parameters), message.Type)
	case 903:
		conn.Response(api.ContractSelectByNoSocket(parameters), message.Type)
	case 904:
		conn.Response(api.ContractSelectByTimeSocket(parameters), message.Type)
	case 905:
		conn.Response(api.ContractAdditionalAddSocket(parameters), message.Type)
	case 906:
		conn.Response(api.ContractAdditionalSelectSocket(parameters), message.Type)
	case 907:
		conn.Response(api.ContractAdditionalUpdateSocket(parameters), message.Type)
	case 908:
		conn.Response(api.ContractAdditionalDeleteSocket(parameters), message.Type)
	case 909:
		conn.Response(api.ContractUpdateSocket(parameters), message.Type)
	case 910:
		conn.Response(api.ContractTypeCreateSocket(parameters), message.Type)
	case 911:
		conn.Response(api.ContractTypeSelectAllSocket(parameters), message.Type)
	case 912:
		conn.Response(api.ContractYearSelectAllSocket(parameters), message.Type)
	case 913:
		conn.Response(api.ContractTypeDeleteSocket(parameters), message.Type)
	case 914:
		conn.Response(api.WorkTypeCreateSocket(parameters), message.Type)
	case 915:
		conn.Response(api.WorkTypeSelectAllSocket(parameters), message.Type)
	case 916:
		conn.Response(api.WorkTypeDeleteSocket(parameters), message.Type)
	case 917:
		conn.Response(api.ContractSelectByVagueSocket(parameters), message.Type)
	default:
		conn.Response(bean.NewErrorMessage(fmt.Sprintf("未知的消息类型%v", message.Type)), 0)
	}
}
